<?php
class ajax_target extends MY_Controller{
    function ajax_target(){
        parent::MY_Controller();
        $this->load->model('m_member');
        $this->load->model('m_sekolah');
        $this->load->model('m_kelas');
        $this->load->model('m_user');
        $this->load->model('m_adik_asuh');
        $this->load->model('m_dropzone');
        $this->load->model('m_relasi_kakak_pembina');
        $this->load->model('m_relasi_donatur_tunggal');
        $this->load->model('m_mata_pelajaran');
        $this->load->model('m_ekstrakulikuler');
        $this->load->model('m_raport');
        $this->load->model('m_jumlah_beasiswa');
        $this->load->model('m_keuangan_adik_asuh');
        $this->load->model('m_tahun_ajaran');
        $this->load->plugin('jpgraph');
    }
    function index(){
        $data = $this->m_sekolah->searchSekolah($_POST['q']);
        die(json_encode($data));
    }
    function searchMemberUser(){
        $data = $this->m_member->searchMemberUser($_POST['q']);
        die(json_encode($data));
    }
    function hapus_keanggotaan($id){
        $delete = $this->m_member->hapusKeanggotaan($id);
        
        die(json_encode(array("status"=>$delete)));
    }
    function selectKelas($idTingkatPendidikan){
        $data = $this->m_kelas->getListKelas($idTingkatPendidikan);
        die(json_encode($data));
    }
    function cekUsername($username = NULL,$idMember = NULL){
        $cek = $this->m_user->cekUsername($username,$idMember);
        
        die(json_encode(array("status"=>$cek)));
    }
    function getAdikAsuhByMember($idMember){
        $data = $this->m_adik_asuh->getAdikAsuhByMember($idMember,$_POST['q']);
        die(json_encode($data));
    }
    function getDropzone(){
        $data = $this->m_dropzone->searchDropzone($_POST['q']);
        die(json_encode($data));
    }
    function searchRelasiKakakPembina(){
        $data = $this->m_relasi_kakak_pembina->searchRelasiKakakPembina($_POST['q']);
        die(json_encode($data));
    }
    function searchRelasiDonaturTunggal(){
        $data = $this->m_relasi_donatur_tunggal->searchRelasiDonaturTunggal($_POST['q']);
        die(json_encode($data));
    }
    function searchDonaturTunggal(){
        $data = $this->m_relasi_donatur_tunggal->searchDonaturTunggal($_POST['q']);
        die(json_encode($data));
    }
    function searchKakakPembina(){
        $data = $this->m_relasi_kakak_pembina->searchKakakPembina($_POST['q']);
        die(json_encode($data));
    }
    function searchDonasiDonaturTunggal(){
        $data = $this->m_relasi_donatur_tunggal->searchDonasiDonaturTunggal($_POST['q'],$_POST['id_member']);
        die(json_encode($data));
    }
    function searchAdikAsuhRaport(){
        $data = $this->m_adik_asuh->getAdikAsuhByMember($_POST['id_member'],$_POST['q']);
        die(json_encode($data));
    }
    function searchAdikAsuh(){
        $data = $this->m_adik_asuh->searchAdikAsuh($_POST['q']);
        die(json_encode($data));
    }
    function searchMataPelajaranByKelas(){
        $data = $this->m_mata_pelajaran->getMataPelajaranByKelas($_POST['id_kelas'],$_POST['q']);
        die(json_encode($data));
    }
    function searchEkstrakulikuler(){
       $data = $this->m_ekstrakulikuler->searchEkstrakulikuler($_POST['q']);
       die(json_encode($data));
    }
    function getAdikAsuhByTingkatPendidikan($idTingkatPendidikan){
        $data = $this->m_adik_asuh->getAdikAsuhByTingkatPendidikan($idTingkatPendidikan);
        die(json_encode($data));
    }
    function getNominalBeasiswa($idTingkatPendidikan){
        $data = $this->m_jumlah_beasiswa->getBeasiswaByTingkatPendidikan($idTingkatPendidikan);
        die(json_encode($data));
    }
    function getAdikAsuhTanpaDonatur(){
        $data = $this->m_adik_asuh->getAdikAsuhTanpaDonatur();
        die(json_encode($data));
    }
    function getSaldoAdikAsuh($idAdikAsuh){
        $data = $this->m_keuangan_adik_asuh->getSaldoAdikAsuh($idAdikAsuh);
        die(json_encode($data));
    }
    function getGrafik(){
        //$_POST['tingkat_pendidikan']
        $kelas = $this->m_kelas->getListKelas();
        $idAdikAsuh = $_POST['idAdikAsuh'];
        if(count($idAdikAsuh) == 0){
            $adikAsuh = 0;
        }else{
            $no = 0;
            foreach ($idAdikAsuh as $row){
                $adikAsuh[] = $this->m_adik_asuh->getAdikAsuhById($row);
                $num = 0;
                foreach ($kelas as $kls){
                    $rata2[$no][$num] = $this->m_raport->getNilaiRata($row,$kls['id']);
                    $kelass[$num] = $kls['nama'].' '.$kls['tingkat_pendidikan'];
                    $num++;
                }
                $no++;
            }
            foreach ($adikAsuh as $key => $rows){
                $namaAdikAsuh[] = $rows[0]['nama'];
            }
            
            $graph = multilinechart($namaAdikAsuh,$kelass,$rata2);
            $graph_temp_directory = 'temp';  
            if($_POST['tingkat_pendidikan'] == 1){
                $graph_file_name = 'grafik_perkembangan_pendidikan_sd.png';
            }else if($_POST['tingkat_pendidikan'] == 2){
                $graph_file_name = 'grafik_perkembangan_pendidikan_smp.png';
            }else $graph_file_name = 'grafik_perkembangan_pendidikan_sma.png';
            
            $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
            if(file_exists($graph_file_location)){
                @unlink($graph_file_location);
            }
            $graph->Stroke('./'.$graph_file_location);
        }
        
        $data = array('kelas' => $kelas,'adikAsuh' => $adikAsuh,'rata2' => $rata2,'graph' => $graph_file_location);
        
        die(json_encode($data));
    }
    function getPerangkinganAdikAsuh($idTingkatPendidikan){
        $tahun_ajaran = $this->m_tahun_ajaran->getActiveTahunAjaran();
        foreach ($tahun_ajaran as $ta);
        $data = $this->m_raport->getRangkingAdikAsuh($idTingkatPendidikan,$ta['id']);
        
        die (json_encode($data));
    }
}
?>
